package com.fitso.model.bean.nutrition;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;

import com.fitso.model.bean.ModelBean;
import com.fitso.model.bean.measure.Measure;

public class Serving extends ModelBean implements Comparable<Serving>, Serializable {
	// Only change on breaking changes, i.e., removing fields
	private static final long serialVersionUID = 1L;
	
	private String _description;

	private Measure _measure;

	private Set<Serving> _subServings = new HashSet<Serving>();

	public Serving() {}
	
	public Serving(Measure measure){
		this(measure, null);
	}

	public Serving(Measure measure, String description) {
		_measure = measure;
		_description = description;
	}

	public int compareTo(Serving other) {
		return new CompareToBuilder().append(_measure, other._measure).append(_description, other._description).append(
				_subServings, other._subServings).toComparison();
	}

	public Set<Serving> getSubServings() {
		return _subServings;
	}

	public void addSubServing(Serving serving) {
		_subServings.add(serving);
	}

	public void setSubServings(Set<Serving> subServings) {
		_subServings = subServings;
	}

	public boolean equals(Object o) {
		if (o == null) {
			return false;
		}
		if (o == this) {
			return true;
		}
		if (o.getClass() != getClass()) {
			return false;
		}

		Serving other = (Serving) o;
		return new EqualsBuilder().append(_measure, other._measure)
				.append(_description, other._description).append(_subServings.containsAll(other._subServings),
						other._subServings.containsAll(_subServings)).isEquals();
	}

	public String getDescription() {
		return _description;
	}

	public Measure getMeasure() {
		return _measure;
	}

	public int hashCode() {
		return new HashCodeBuilder().append(_measure).append(_description).append(_subServings).toHashCode();
	}

	public void setDescription(String description) {
		_description = description;
	}

	public void setMeasure(Measure measure) {
		_measure = measure;
	}

	public String toString() {
		return new ReflectionToStringBuilder(this).toString();
	}
}